﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pub.Class;
using System.Web;

namespace GTS.MiniBox.Page {
    public static class SiteConfig {
        /// <summary>
        /// 加密和解密的KEY
        /// </summary>
        public readonly static string Key = WebConfig.GetApp("Key");
        /// <summary>
        /// 日志保存路径
        /// </summary>
        public readonly static string LogPath = WebConfig.GetApp("LogPath").GetMapPath();
        /// <summary>
        /// 通堂使用批次
        /// </summary>
        public readonly static string TongTangOrigin = WebConfig.GetApp("TongTangOrigin");
        /// <summary>
        /// 通堂使用批次
        /// </summary>
        public readonly static string TongTangUrl = WebConfig.GetApp("TongTangUrl");
        /// <summary>
        /// 通堂计费URL
        /// </summary>
        public readonly static string TongTangChargeUrl = WebConfig.GetApp("TongTangChargeUrl");
        /// <summary>
        /// charge_url节点
        /// </summary>
        public readonly static string charge_url = WebConfig.GetApp("charge_url");
        /// <summary>
        /// double_url节点
        /// </summary>
        public readonly static string double_url = WebConfig.GetApp("double_url");
        /// <summary>
        /// 是否启用日志
        /// </summary>
        public readonly static bool UseLog = WebConfig.GetApp("UseLog").ToBool();
        /// <summary>
        /// 写系统出错日志
        /// </summary>
        /// <param name="msg">Exception</param>
        public static void WriteLog(Exception msg) {
            WriteLog(msg.ToExceptionDetail());
        }
        /// <summary>
        /// 写日志 主要用在非web项目中
        /// </summary>
        /// <param name="msg">string</param>
        public static void WriteLog(string msg) {
            if (!UseLog) return;
            string LogFile = LogPath.TrimEnd('\\') + @"\Log_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("/*******************************************************************************************************");
            sb.AppendLine(string.Format("* DateTime：{0}	", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            if (HttpContext.Current.IsNotNull()) {
                sb.AppendLine("* Url：" + Request2.GetUrl());
                sb.AppendLine("* Data：" + Request2.GetRequestInputStream());
            }
            sb.AppendLine("* Message：" + msg);
            sb.AppendLine("*******************************************************************************************************/");
            sb.AppendLine("");
            try { Log.Write(LogFile, sb.ToString()); } catch { }
        }
        /// <summary>
        /// 写日志 主要用在非web项目中
        /// </summary>
        /// <param name="msg">string</param>
        public static void WriteTongTangLog(string msg) {
            if (!UseLog) return;
            string LogFile = LogPath.TrimEnd('\\') + @"\Log_TongTang_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("/*******************************************************************************************************");
            sb.AppendLine(string.Format("* DateTime：{0}	", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            if (HttpContext.Current.IsNotNull()) {
                sb.AppendLine("* Url：" + Request2.GetUrl());
                sb.AppendLine("* Data：" + Request2.GetRequestInputStream());
            }
            sb.AppendLine("* Message：" + msg);
            sb.AppendLine("*******************************************************************************************************/");
            sb.AppendLine("");
            try { Log.Write(LogFile, sb.ToString()); } catch { }
        }
        /// <summary>
        /// 写日志并终止执行
        /// </summary>
        /// <param name="msg">日志</param>
        public static void WriteLogEnd(string msg) {
            WriteLog(msg);
            Msg.WriteEnd(msg);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="RequestInstance"></param>
        /// <param name="ResponseInstance"></param>
        /// <param name="FileName"></param>
        /// <param name="Buffer"></param>
        /// <returns></returns>
        public static bool ResponseGameInfo(HttpRequest RequestInstance, HttpResponse ResponseInstance, string FileName, Byte[] Buffer) {
            string MIMEType = "application/octet-stream";
            try {
                ResponseInstance.AddHeader("Accept-Ranges", "bytes");
                ResponseInstance.Buffer = true;
                long filelength = Buffer.Length;
                long startBytes = 0;
                if (RequestInstance.Headers["Range"] != null) {
                    ResponseInstance.StatusCode = 206;
                    string[] range = RequestInstance.Headers["Range"].Split("=-".ToCharArray());
                    startBytes = Convert.ToInt64(range[1]);
                }
                ResponseInstance.AddHeader("Content-Length", (filelength - startBytes).ToString());
                if (startBytes != 0) {
                    ResponseInstance.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", startBytes, filelength - 1, filelength));
                }
                ResponseInstance.AddHeader("Connection", "Keep-Alive");
                ResponseInstance.ContentType = MIMEType;
                ResponseInstance.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
                List<Byte> listinfo = new List<byte>();
                for (long i = startBytes; i < Buffer.Length; i++) {
                    listinfo.Add(Buffer[i]);
                }
                if (listinfo.Count > 0) ResponseInstance.BinaryWrite(listinfo.ToArray());
                return true;
            } catch (Exception ex) {
                SiteConfig.WriteLog(ex);
                return false;
            }

        }
    }
}
